<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\KeshiModel;

class KeshiController extends Controller {

	protected $instance = NULL;
	
	public function __construct()
	{
		parent::__construct();
		if($this->instance == NULL) {
			$this->instance = new KeshiModel;
		}		
	}
	
	public function index()
	{
		$count      = $this->instance->count();	
		$Page       = new \Think\Page($count,10);
		$Page->setConfig('theme',"<ul class='pagination'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><span> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页 </span></li></ul>");
		$Pageshow       = $Page->show();		
		$index = [
					"limit"	=> array(
							"firstRow" => $Page->firstRow,
							"listRows" => $Page->listRows,
						),
					];
		$keshi = $this->instance->index($index);
		$this->assign('keshi',$keshi);
		$this->assign('Pageshow',$Pageshow);
		$this->display();
	}
	
	// 显示添加科室界面
	public function addKeshiPage()
	{
		$HospitalModel = M('hospital');
		$Hospital	= $HospitalModel->where('status =1')->select();
		
		$this->assign('Hospital',$Hospital);
		return $this->display();
	}
	
	// 添加科室
	public function addKeshi()
	{
		$KeshiModel = M('keshi');
		$keshi['name'] = I('post.name');		
		if (!$KeshiModel->create($keshi,1)) {
			return $this->success('添加失败', U('Admin/Keshi/index'));
		}else{
			$keishi_id = $KeshiModel->add();
			
			$KeshiHospitalModel = M('hospital_keshi');
			$KeshiHospital['keshi_id'] = $keishi_id;
			$KeshiHospital['hospital_id'] = I('post.hospital_id');			
			if (!$KeshiHospitalModel->create($KeshiHospital,1)) {
				return $this->success('添加失败', U('Admin/Keshi/index'));
			}else {
				$KeshiHospitalModel->add();
			}
			
			$UserModel = M('user_hospital');
			$user['phone'] = I('post.phone');
			$user['password'] = MD5(C("PASSWORD_SALT").I("post.password"));
			$user['hospital_id'] = I('post.hospital_id');
			$user['keshi_id'] = $keishi_id;
			$user['createtime'] = time();
			
			if (!$UserModel->create($user,1)) {
				return $this->success('添加失败', U('Admin/Keshi/index'));
			}else {
				$UserModel->add();
			}
			return $this->success('添加成功', U('Admin/Keshi/index'));
		}
	}
	
	// 显示编辑科室界面
	public function editKeshiPage()
	{
		$id = I('get.id');
		$HospitalModel = M('hospital');
		$Hospital	= $HospitalModel->where('status =1')->select();
		
		$KeshiModel = M();
		$SQL = "select a.*, b.name as keshi_name, c.name as hospital_name from hospital_keshi as a 
		left join keshi as b on a.keshi_id = b.id
        left join hospital as c on a.hospital_id = c.id
		where a.keshi_id = " .$id;
		
		$Keshi = $KeshiModel->query($SQL);	
		$this->assign('Hospital',$Hospital);
		$this->assign('Keshi',$Keshi[0]);
		return $this->display();
	}
	
	// 编辑科室
	public function editKeshi()
	{
		$keshihospital['id'] = I('post.id');
		
		$keshi['id'] = I('post.keshi_id');
		$keshi['name'] = I('post.name');
		
		$hospital['hospital_id'] = I('post.hospital_id');
		$KeshiModel = M('keshi');
		
		if (!$KeshiModel->create($keshi,2)) {
			return $this->success('修改失败', U('Admin/Keshi/index'));
		}else{
			$status = $KeshiModel->save();
			
			$KeshiHospitalModel = M('hospital_keshi');
			$KeshiHospital['hospital_id'] = I('post.hospital_id');			
			
			$SQL = "update hospital_keshi set hospital_id= ".$KeshiHospital['hospital_id']." where id =".$keshihospital['id'];
			
			$ret = $KeshiHospitalModel->query($SQL);
			if($ret === false) {
				return $this->success('修改失败', U('Admin/Keshi/index'));
			}
			return $this->success('修改成功', U('Admin/Keshi/index'));
		}
	}
	
}